﻿weapons=new Array();
amm=5000;
timers=new Array();
function attack(obj,weapon,dmg){
	
	
	if(mob.length>0){
	amm+=1;
	
	for(v=mob.length-18;v<=mob.length;v++){
		if(mob[v].hp>0 && mob[v]._y>0){
			_root.purpes=mob[v];
		}
	}
	if(_root.purpes.hp>0){
		weapons[num_unit]=attachMovie(weapon,"weapon"+amm,amm,{dmg:dmg,_x:unit[obj]._x-(unit[obj]._width/2),_y:unit[obj]._y});
	
		
		
	osx2=weapons[num_unit]._x;
	osx1=_root.purpes._x;
	osy2=weapons[num_unit]._y;
	osy1=_root.purpes._y;
	
	osx=osx1-osx2;
	osy=osy2-osy1;
	X=osx/osy;
	Y=osy/300;
	

	if(Y>0){
		Y=-Y;
	}
	
	dist=Math.sqrt((osx2-osx1)*(osx2-osx1)+(osy2-osy1)*(osy2-osy1));
	if(osx1>osx2){
	angel=(Math.atan2(osy1,osx1)*180/Math.PI)+45;
	
	}else{
		angel=Math.atan2(osy1,osx1)*180/Math.PI-45;
		
		
	}
	
	if(osy1>150 && osx1<180 && osx2>osx1){
		
		
		angel=Math.atan2(osy1,osx1)*180/Math.PI-135;
		
			
	
	
	}
	
	//angel = 57.29577957855229 * Math.atan2(osy, osx);
	
		weapons[num_unit]._rotation=angel;
		unit[obj].gotoAndPlay(2);
		sounds("bow_shot.wav");
		unit[obj]._rotation=angel;
		timers[amm]=setInterval(weap,40,obj=weapons[num_unit],dmg,X,Y);
		setTimeout(clearInterval,5000,timers[amm]);
		setTimeout(remove,5000,weapons[num_unit]);
	}
	
	}
}

function weap(obj,dmg,X,Y){
	
	obj._y+=Y*10;
	obj._x+=X*10;
	
	for(i=mob.length-15;i<=mob.length;i++){
		if(mob[i].hitTest(obj)){
			
			removeMovieClip(obj);
			
			
			
			mob[i].hp-=dmg;
			sounds("arrow_shot.wav");
			
			hp=20/l_green[i+200].hp;
			l_green[i+200]._width=mob[i].hp*hp;
			if(mob[i].hp<=0)
			{
			blood[num_blood]=attachMovie("blood","blood"+num_blood,num_blood,{_x:mob[i]._x,_y:mob[i]._y});
			setInterval(remove,5000,obj=blood[num_blood]);
			num_blood+=1;
			mob[i].gotoAndPlay(8);
			mob[i].sp=0;
			setTimeout(remove,400,mob[i]);
			setTimeout(remove,400,l_green[i+200]);
			setTimeout(remove,400,l_red[i+100]);
			_root.kill+=1;
			_global.kill_score+=1;
			t_kill.text=kill;
			add_gold(gold=5);
			}
		}
	}
	
	if(obj._y<-15){
		removeMovieClip(obj);
	}
	
}




